home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Interfaces / Universal Interfaces 2.0a3 / CIncludes / iostream.h < prev    next >
Encoding:
Text File  |  1994-12-14  |  41.9 KB  |  1,240 lines  |  [TEXT/MPS ]

  1. //     IOStreams Package
  2. //     Steve Teale April 1993
  3. //     Copyright Symantec Corp 1990-1994. All Rights Reserved.
  4. //
  5. //    Some portions Copyright © 1994 Apple Computer Inc.
  6.  
  7. #ifndef __IOSTREAM__
  8. #define __IOSTREAM__
  9.  
  10. #include <stddef.h>
  11.  
  12.  
  13. //    The following code was removed from 2comp.h and added here to deal with 
  14. //    changes from the ANSI spec for this header. 2comp.h is a Symantec header
  15. //    required for the libraries build as well as usage. It may be eliminated
  16. //    in a future version if changes to the IOStreams library are performed.
  17. //    rjd • 941130
  18. //
  19.  
  20. #ifndef __2COMP_HPP        // required for IOStream libraries build
  21. #define __2COMP_HPP
  22.  
  23. struct _2c_translate_info {
  24.     char *cp;
  25.     char digits[20];
  26.     int neg, radix, dcount, bytes;
  27. };
  28.  
  29. class ostream;
  30. class istream;
  31.  
  32. class _2Comp {
  33. friend class ostream;
  34. friend class istream;
  35.     static ostream &insert(ostream&, const void *body, int bytes, int issigned);
  36.     static int format(unsigned char *body, char *buf, int bytes,
  37.                         int radix, int upper, int negative);
  38.     static istream &extract(istream &is, void *body, int bytes);
  39.     static int _2Comp::xlate(char *s, unsigned char *body,
  40.                         int radix, int negative, int bytes);
  41.     static int div(unsigned char *body, int d, int bytes);
  42.     static void negate(unsigned char *body, int bytes);
  43. };
  44.  
  45. //    End of 2comp.h inclusion
  46.  
  47. #endif    // __2COMP_HPP • End of 2comp.h inclusion
  48.  
  49.  
  50. #define seek_dir relative_to
  51.  
  52. #ifndef EOF
  53. const int EOF = -1;
  54. #endif
  55.  
  56. const int _ios_default_decimal_precision = 6;
  57. const int _ios_n_extended_format_words = 10;
  58. // This is the number of extended format state words reserved for use
  59. // by derived classes and user inserters. This value should be reasonably
  60. // small, as it inflates the size of each instance of ios.
  61.  
  62. class streampos {
  63. friend class streamoff;
  64. public:
  65.     streampos(long elems, size_t elemsize = 1) : ne(elems), es(elemsize) {}
  66.     operator long() const { return (ne == -1)? EOF: ne*es; }
  67. private:
  68.     long ne;
  69.     size_t es;
  70. };
  71.  
  72. class streamoff {
  73. public:
  74.     streamoff(long elems, size_t elemsize = 1) : ne(elems), es(elemsize) {}
  75.     streamoff(streampos &a) : ne(a.ne), es(a.es) {}
  76.     size_t stepsize() const { return es; }
  77.     long steps() const { return ne == -1? 0: ne; }
  78.     streamoff& operator += (long o) { ne += o; return *this; }
  79.     streamoff& operator -= (long o) { ne -= o; return *this; }
  80.     operator long() const { return steps()*es; }
  81.  
  82. private:
  83.     long ne;
  84.     size_t es;
  85. };
  86.  
  87. class streambuf;
  88. class ostream;
  89. class istream;
  90.  
  91.  
  92. class ios {
  93.  
  94. // This is the base class for istream and ostream, and all of their
  95. // derivations.
  96.  
  97. friend ostream &endl(ostream &);
  98.  
  99. public:
  100.     enum io_state {
  101.         goodbit=0,
  102. // No errors - everything hunky dory!
  103.         eofbit=1,
  104. // Normally set when underflow failed because there was no more file.
  105.         failbit=2,
  106. // An error has ocurred, but it is probably recoverable, and the
  107. // stream is still in a useable state
  108.         badbit=4
  109. // A fatal error has ocurred
  110.     };
  111.  
  112. // This is called seek_dir in the AT & T version, which is misleading.
  113. // A define is included above for compatibility. These enumerators are
  114. // used to specify relative seeks in streams.
  115.     enum relative_to {
  116.         beg,
  117. // For seek operations relative to the beginning of the stream (file),
  118.  
  119.         cur,
  120. // relative to the current position in the stream (file),
  121.  
  122.         end
  123. // and relative to the end of the stream (file)
  124.     };
  125.  
  126. // The following enumeration applies to file related derivatives.
  127.     enum open_mode {
  128.         in=0x1,
  129. // Input allowed
  130.  
  131.         out=0x2,
  132. // Output allowed
  133.  
  134.         ate=0x4,
  135. // A seek to the end of the file to be performed during open.
  136.  
  137.         app=0x8,
  138. // All writes are to the end of the file - implies out
  139.  
  140.         trunc=0x10,
  141. // Existing contents of the file to be discarded. Implies out
  142. // unless ate or app specified as well.
  143.  
  144.         nocreate=0x20,
  145. // Open will fail if the file does not already exist
  146.  
  147.         noreplace=0x40,
  148. // Open will fail if the file does already exist
  149.  
  150.         translated = 0x80
  151. // CR/LF pairs to be translated to newline characters on input
  152. // and newline characters to be translated to CR/LF pairs on
  153. // output (the normal behaviour for DOS)
  154.     };
  155.  
  156. // The formatting state is a bit-mask used to control some of the
  157. // inserters and extractors. All of the bits of the format state
  158. // can be manipulated by flags(), setf(), and unsetf(). Some
  159. // specialized parts of the formatting state can be manipulated
  160. // by fill(), width(), and precision() . Here are the meanings
  161. // of the various bits:
  162.  
  163.     enum format_mode {
  164.         skipws = 0x1,
  165. // Skip past leading white space when extracting.
  166. // Since zero-width fields are considered an
  167. // error by the numeric extractors, attempting
  168. // to extract white-space into a number without
  169. // this bit set will set an error flag.
  170.  
  171.         left = 0x2,
  172. // Left-adjust values when inserting (fill on the right).
  173.  
  174.         right = 0x4,
  175. // Right-adjust values when inserting (fill on the left).
  176.  
  177.         internal = 0x8,
  178. // When inserting, fill _between_ the numeric sign or
  179. // base indicator and the value.
  180.  
  181.         dec = 0x10, oct = 0x20, hex = 0x40,
  182. // Default radix for integers. If neither dec, octal,
  183. // or hex is set, integer inserters use base 10, and
  184. // integer extractors interpret numbers according to the
  185. // C++ lexical convention: "0x" precedes a base-16 number,
  186. // and a number with a leading zero is base-8.
  187.  
  188.         showbase = 0x80,
  189. // If this is set, base-16 numbers will be inserted with a
  190. // leading "0x", and base-8 numbers will have a leading zero.
  191.  
  192.         showpoint = 0x100,
  193. // If this is set, the floating-point inserters will print a
  194. // decimal point and trailing zeroes, even when the trailing
  195. // places are not significant.
  196.  
  197.         uppercase = 0x200,
  198. // If this is set, "E" instead of "e" will be used to indicate
  199. // the exponent of a floating point number, and "A" through "F"
  200. // will be used to represent base-16 numerals instead of "a"
  201. // through "f".
  202. //
  203. // If uppercase and showbase are both set, the string "0X"
  204. // instead of "0x" will be used to indicate a base-16 number.
  205.  
  206.         showpos = 0x400,
  207. // If this is set, positive numbers will be inserted with a
  208. // leading "+".
  209.  
  210.         scientific = 0x800,
  211. // If this is set, the floating-point inserters will print a
  212. // number with one digit before the decimal point, and the
  213. // number of digits after the decimal point equal to the
  214. // value of precision(). The character "e" will introduce the
  215. // exponent.
  216.  
  217.         fixed = 0x1000,
  218. // If this is set, the floating-point inserters will use
  219. // precision() to determine the number of digits after the
  220. // decimal point.
  221. //
  222. // If neither scientific or fixed is set, numbers with
  223. // exponents smaller than -4 or greater than precision() will
  224. // be printed as if scientific were set. Other numbers will
  225. // be printed using zeroes to explicitly show the decimal place.
  226.  
  227.         unitbuf = 0x2000,
  228. // When this is set, a flush is performed after each insertion
  229. // (by ostream::osfx()). This is more efficient than using
  230. // unbuffered output, but provides most of the same advantages.
  231.  
  232.         stdio = 0x4000
  233. // When this is set, streams using stdiobufs will flush stdout and
  234. // stderr after each insertion.
  235.  
  236. // Note that it is not possible to use bit 0x8000 in this way,
  237. // as this evaluates to an enumerator with a negative value and
  238. // lots of bits set.
  239.     };   // end enum format_mode
  240.  
  241.     static const long stickywidth;
  242.     static const long spacing;
  243.  
  244.     enum format_mode_mask {
  245.         defaults = right|skipws,
  246.         basefield = dec|oct|hex,
  247.         adjustfield = left|right|internal,
  248.         floatfield = scientific|fixed
  249.     };
  250.  
  251. public: // just a reminder!
  252.  
  253.     ios(streambuf *buffer);
  254. // Construct an ios associated with the argument streambuf.
  255. // "buffer" should not be null.
  256.  
  257.     virtual ~ios();
  258.  
  259. /////////////////////////////////////////////////////////////
  260. //
  261. // Functions to interrogate/set the error state
  262.  
  263.     int good() const { return error_state == 0; };
  264. // If there are no error bits set, this returns non-zero, otherwise
  265. // it returns zero.
  266.  
  267.     int eof() const { return error_state & eofbit; };
  268. // If eofbit is set in the error state, this return non-zero, otherwise
  269. // it returns zero. This indicates that end-of-file has been reached
  270. // while reading the character stream.
  271.  
  272.     int fail() { return error_state & (badbit | failbit); }
  273. // If badbit or failbit is set in the error state, return non-zero.
  274. // Otherwise, return zero. Failbit generally indicates that some
  275. // extraction has failed, but the stream may still be used once
  276. // failbit has been cleared.
  277.  
  278.     int bad() const { return error_state & badbit; }
  279. // Returns non-zero if badbit is set in the error state. This
  280. // indicates some unrecoverable error, generally an I/O error.
  281.  
  282.     int operator!() const
  283.     {
  284.         return error_state & (badbit|failbit);
  285.     }
  286. // Return non-zero if badbit or failbit is set in the error state,
  287. // which allows expressions of the form:
  288. //  if ( !cout )
  289.  
  290.     operator void*()
  291.     {
  292.         return (error_state & (badbit|failbit))?
  293.             0: this;
  294.     }
  295. // Convert an ios to a value that can be compared to zero, but can
  296. // not be easily used accidentally. The return value is a void pointer
  297. // that will be zero if failbit or badbit is set in the error state,
  298. // and non-zero otherwise. This allows an iostream to be used
  299. // in conditional expressions that test its state, as in:
  300. // if ( cin ) and if ( cin >> variable )
  301.  
  302.     int rdstate() const { return error_state; };
  303. // Returns the current error state.
  304.  
  305.     void clear(int new_state = 0) { error_state = new_state; }
  306. // Stores "new_state" as the error state.
  307. //
  308. // To set a bit without clearing others requires something like
  309. // clear(bits_to_set | rdstate()) .
  310.  
  311. //////////////////////////////////////////////////////////////////
  312. //
  313. // Functions to set/interrogate various options
  314.  
  315.     char fill(char new_value)
  316.     {
  317.         char old_value = padding_character;
  318.         padding_character = new_value;
  319.         return old_value;
  320.     }
  321. // Sets the fill character, and returns the old value. This is the
  322. // character used to pad output when the left, right, or internal
  323. // adjustments are in effect.
  324.  
  325.     char fill() const { return padding_character; }
  326. // Returns the fill character.
  327.  
  328.     int precision(int new_value)
  329.     {
  330.         int old_value = decimal_precision;
  331.         decimal_precision = new_value >= 0?
  332.             new_value: _ios_default_decimal_precision;
  333.         return old_value;
  334.     }
  335. // Sets the decimal precision, and returns the old value.
  336. // This controls the number of digits inserted after the decimal
  337. // point by the floating-point inserter.
  338.  
  339.     int precision() const { return decimal_precision; }
  340. // Returns the current decimal precision.
  341.  
  342.     ostream *tie(ostream * new_value)
  343.     {
  344.         ostream *old_value = tied_ostream;
  345.         tied_ostream = new_value;
  346.         return old_value;
  347.     };
  348. // Facilitate automatic flushing of iostreams.
  349. // Associate this ios to an ostream such that the ostream will be
  350. // flushed before this ios makes a request for characters, or flushes
  351. // output characters. These ties exist by default:
  352. //  cin.tie(cout);
  353. //  cout.tie(cerr);
  354. //  cout.tie(clog);
  355. //
  356. // For other instances of ios, the tie is set to zero by default.
  357. //
  358. // This returns the old value.
  359.  
  360.     long flags() const { return format_state; }
  361. // Return the format state flags.
  362.  
  363.     long flags(long new_value)
  364.     {
  365.         long old_value = format_state;
  366.         format_state = new_value;
  367.         return old_value;
  368.     }
  369. // Sets the format state flags, returning the old values.
  370.  
  371.     long setf(long bits_to_set, long mask = 0)
  372.     {
  373.         long old_value = format_state;
  374.         format_state = (format_state & ~mask)
  375.                 | bits_to_set;
  376.         return old_value;
  377.     }
  378. // Clears the bits corresponding to mask in the format state, and
  379. // then sets only those bits from the value of bits_to_set.
  380.  
  381.     long unsetf(long bits_to_clear)
  382.     {
  383.         long old_value = format_state;
  384.         format_state &= ~bits_to_clear;
  385.         return old_value;
  386.     };
  387. // Clears flags in the format state.
  388.  
  389.     ostream *tie() const { return tied_ostream; };
  390. // Return the current "tied" ostream.
  391.  
  392.     int width(int new_value);
  393. // Sets the field width used by inserters.
  394. // When the width is zero, inserters will use only as many characters
  395. // as necessary to represent a value. When the width is non-zero,
  396. // inserters will insert at least that many characters, using the
  397. // fill character to pad out the field. Inserters will never truncate,
  398. // so they may output more characters than the current width.
  399.  
  400.     int width() const { return field_width; };
  401. // Returns the current width.
  402.  
  403. ////////////////////////////////////////////////////////////////
  404. //
  405. // Other utilities
  406.  
  407.     streambuf *rdbuf() { return buf; }
  408. // Returns a pointer to the streambuf associated with this ios.
  409.  
  410.     static void sync_with_stdio();
  411. // Use this when mixing C and C++ in the same program.
  412. // It resets cin, cout, cerr, and clog to use stdiobufs, and
  413. // thus makes I/O to these streams compatible with the C stdio.
  414. // Invoking this degrades performance.
  415.  
  416. ////////////////////////////////////////////////////////////////
  417. //
  418. // Functions to manipulate user defined format flags and control
  419. // words.
  420.  
  421.     static int bitalloc();
  422. // Returns an int with one previously-unallocated bit set.
  423. // This allows users who need an additional format flag to
  424. // get one. Note that this allocation is global for class ios,
  425. // not local to any instance of class ios.
  426.  
  427.     static int xalloc();
  428. // Returns a previously-unused index into an array of words usable
  429. // as format-state variables by derived classes.
  430.  
  431.     long &iword(int index)
  432.     {
  433.         return extended_format_words[index].l;
  434.     }
  435. // Returns a reference to one of the auxillary format state words
  436. // reserved for use by derived classes and user inserters, where
  437. // index is a value returned by xalloc().
  438.  
  439.     void* &pword(int index)
  440.     {
  441.         return extended_format_words[index].vp;
  442.     };
  443.  
  444. // Returns a reference to a pointer to one of the auxillary format
  445. // state words reserved for use by derived classes and user
  446. // inserters, where index is a value returned by xalloc().
  447.  
  448. protected:
  449.     void set_buffer(streambuf *b) { buf = b; }
  450.     void init(streambuf *);
  451.     ios();
  452.  
  453. private:
  454.     streambuf   *buf;
  455.     ostream     *tied_ostream;
  456.     long        format_state;
  457.     char        error_state;
  458.     char        padding_character;
  459.     short       decimal_precision;
  460.     short       field_width;
  461.     union {
  462.         void *vp;
  463.         long l;
  464.     } extended_format_words[_ios_n_extended_format_words];
  465.  
  466.     ios(const ios&);
  467.     ios &operator=(const ios&);
  468. // These copying functions are private so that the compiler
  469. // will complain about an attempt to assign an ios. It is not
  470. // actually defined. Instead of copying an ios, assign a pointer
  471. // to it.
  472.  
  473. };
  474.  
  475. ////////////////////////////////////////////////////////////////
  476. //
  477. // Manipulator functions
  478.  
  479. ios &dec(ios&);
  480. // Set the default integer radix to 10.
  481. // Invoke this (and the other manipulators) this way:
  482. // stream >> dec;
  483. // stream << dec;
  484.  
  485. ios &hex(ios&);
  486. ios &oct(ios&);
  487. ios &defaults(ios&);
  488.  
  489. class streambuf {
  490. // Streambuf abstracts the operations used to perform I/O on a character
  491. // stream such as a computer terminal, a file or a string.
  492. //
  493. // The two basic operations are "get", which fetches characters from the
  494. // stream, and "put", which places characters on the stream. Some streambufs
  495. // are unidirectional, in which case they support the "get" operation but
  496. // not "put", or vice-versa.
  497. //
  498. // Some streambufs are bi-directional, with the "get" and "put" offsets
  499. // at different locations in the stream. Some streambufs may implement
  500. // a circular buffer between the "get" and "put" offsets, as in a FIFO.
  501. // Some streambufs lock the "get" and "put" offset together, as in a UNIX file.
  502. // Some streambufs provide buffered I/O on the underlying character streams
  503. // for efficiency.
  504. //
  505. // Streambufs contain a buffer, a get area, and a put area. Usually, the
  506. // get area and put area overlap the buffer, but do not overlap each other.
  507.  
  508. public:
  509.  
  510.     streambuf();
  511. // The default constructor.
  512.  
  513.     streambuf(char *memory, int length);
  514. // Constructs a streambuf, possibly using the argument buffer.
  515.     virtual ~streambuf();
  516.  
  517.     void dbp();
  518. // Write debugging information about the streambuf on file
  519. // descriptor 1. Nothing about the form of that information
  520. // is specified.
  521.  
  522.     int in_avail() const { return _egptr - _gptr; }
  523. // Return the number of characters that can be read immediately
  524. // with a guarantee that no errors will be reported.
  525.  
  526.     int out_waiting() const { return _pptr - _pbase; }
  527. // Return the number of characters that have not
  528. // been flushed away, or EOF if there are no characters.
  529.  
  530.     virtual streambuf *setbuf(char *memory, int length);
  531. // Requests that this streambuf use the argument memory buffer.
  532. // Special case: a null memory argument, or a length argument that is
  533. // zero or negative, is taken as a request that this streambuf be
  534. // unbuffered.
  535. //
  536. // If the request to set the buffer is honored, return "this",
  537. // otherwise return 0.
  538. //
  539. // * Use of this interface, except by a derived class of streambuf,
  540. // is discouraged. It is not protected for compatibility with the
  541. // original stream package in Stroustrup.
  542. //
  543. // The default version of this member will honor the request if no
  544. // buffer has been allocated.
  545.  
  546.     virtual streampos  seekpos(streampos position,
  547.                                     int which=ios::in|ios::out);
  548. // Performs an absolute seek of the get or put pointers, or both, to
  549. // "position". Position is an implementation-dependent value which
  550. // should not have arithmetic performed on it. "which" signifies what
  551. // pointer is to be affected: ios::in signifies the get pointer, and
  552. // ios::out signifies the put pointer. The two "which" values may be
  553. // or-ed together, in which case the operation affects both pointers.
  554. //
  555. // * The default version of this member returns
  556. //       seekoff(position, ios::beg, which)
  557. // thus is is only necessary for a derived class to define
  558. // seekoff(), and use the inherited seekpos().
  559.  
  560.     virtual streampos seekoff(streamoff offset, ios::relative_to pos,
  561.                                     int which=ios::in|ios::out);
  562. // Performs a relative seek of the get or put pointers, or both, by
  563. // "offset". Position is a byte offset, and may be negative.
  564. // "pos" may be ios::beg, which signifies a seek relative to
  565. // the beginning of the stream; ios::cur, which signifies a seek
  566. // relative to the current position in the stream; and ios::end, which
  567. // signifies a seek relative to the end of the stream.
  568. // "which" signifies what pointer is to be affected: ios::in
  569. // signifies the get pointer, and ios::out signifies the put pointer.
  570. // The two "which" values may be or-ed together, in which case the
  571. // operation affects both pointers.
  572. //     * The default version of this member always returns EOF.
  573.  
  574.     int sgetc()
  575.     {
  576.         return _gptr < _egptr?
  577.                     (unsigned char) *_gptr: underflow();
  578.     }
  579. // Returns the character at the get pointer, without moving the get
  580. // pointer.
  581.  
  582.     int sbumpc()
  583.     {
  584.         return _gptr < _egptr?
  585.                     (unsigned char) *_gptr++: sbumpc_special();
  586.     }
  587. // Moves the get pointer forward one character, and return the
  588. // character it moved past.
  589.  
  590.     int sgetn(char *buffer, int count);
  591. // Fetch the "count" characters following the get pointer, and
  592. // stores them in "buffer". If there are less than "count" characters,
  593. // the number remaining are fetched. The get pointer is repositioned
  594. // after the fetched characters, and the number of characters fetched
  595. // is returned.
  596.  
  597.     int snextc()
  598.     {
  599.         return _gptr+1 < _egptr?
  600.                     ((unsigned char) *(++_gptr)): underflow();
  601.     }
  602.  
  603. // Skip past the character at the get pointer, and return the
  604. // character after that, or EOF.
  605.  
  606.     int sputbackc(char c)
  607.     {
  608.         return _gptr > _gbase?
  609.                     (*(--_gptr) = c, 0): pbackfail(c & 0x00ff);
  610.     }
  611. // Move the get pointer back one character. If c is not the last
  612. // character retrieved, the effect is undefined. In this implementation
  613. // it should work
  614.  
  615.     int sputc(int c)
  616.     {
  617.         return _pptr < _epptr?
  618.                     (*_pptr++ = c, 0): overflow(c & 0x00ff);
  619.     }
  620. // Store c on the character stream, advancing the "put" pointer.
  621. // Return EOF when an error occurs.
  622.  
  623.     int sputn(const char *string, int length);
  624. // Store "n" characters on the character stream, advancing the "put"
  625. // pointer past the stored characters. Return the number of characters
  626. // stored, which may be less than "n" if there is an error.
  627.  
  628.     void stossc()
  629.     {
  630.         if (_gptr < _egptr) ++_gptr;
  631.     }
  632. // Skip the "get" pointer forward, wasting one character of input.
  633. // If the get pointer is already at the end of the sequence
  634. // this has no effect.
  635.  
  636.     virtual int sync();
  637. // Make the external character stream and the streambuf consistent
  638. // with each other. This usually means:
  639. // 1. If there are characters in the get area, return them to the
  640. //    stream, or throw them away. Re-position the stream so that
  641. //    it appears that the characters that had been in the get area
  642. //    had never been taken from the stream.
  643. // 2. If there are characters in the put area, store them to the
  644. //    stream. Re-position the stream immediately after the characters
  645. //    just stored.
  646. // 3. Return EOF if there's an error, otherwise return some other
  647. //    value.
  648. //     * The default version of this member will return 0 if the get area
  649. // is empty, otherwise it returns EOF.
  650.  
  651.     virtual int overflow(int c = EOF);
  652. // This is called to store characters in the character stream, usually
  653. // when the put area is full. It generally stores all of the characters
  654. // that are in the put area, stores c if it is not EOF, and calls
  655. // setp() to establish a new put area. It should return EOF if it
  656. // has an error, otherwise return some other value.
  657.  
  658.     virtual int underflow();
  659. // This is called to read characters from the character stream,
  660. // usually when the get area is empty. It should read one or more
  661. // characters, and place them in the get area. It should return the
  662. // first character in the get area, without incrementing the get
  663. // pointer. It should return EOF if there's a problem.
  664.  
  665. protected:
  666.  
  667. // Buffer base and one past the end
  668.     char *base() const { return _base; }
  669.     char *ebuf() const { return _ebuf; }
  670. // Get pointer, pushback limit, and one past end of put area
  671.     char *gptr() const { return _gptr; }
  672.     char *eback() const { return _gbase; }
  673.     char *egptr() const { return _egptr; }
  674. // Put pointer, and one past end of put area
  675.     char *pbase() const { return _pbase; }
  676.     char *pptr() const { return _pptr; }
  677.     char *epptr() const { return _epptr; }
  678.  
  679.     int blen() const { return _ebuf-_base; }
  680. // Return the size in characters of the buffer.
  681.  
  682.     int allocate();
  683. // Try to set up the buffer. If one already exists or unbuffered() is
  684. // non-zero, return 0 without doing anything. If something goes wrong,
  685. // return EOF. Otherwise, return 1. This function is only called by
  686. // virtual members of the base class streambuf, thus you can override
  687. // everything that uses it.
  688.  
  689.     virtual int doallocate();
  690. // This is called when allocate() determines that space is needed.
  691. // This function must call setb() to set up the buffer, and return
  692. // a non-eof value, or return EOF if it can not get space.
  693. // This function is only called if unbuffered() is zero, and base()
  694. // is zero.
  695. //
  696. // * The default version allocates a buffer using operator new.
  697.  
  698.     virtual int pbackfail(int c);
  699. // This is called by sputbackc() when the get pointer is equal to
  700. // the base of the get area, and there is thus no space to put back
  701. // characters. It may try to re-arrange the buffer so that it is
  702. // possible to put back characters, and then put back c. If it
  703. // succeeds it should return c, otherwise it should return EOF.
  704. //     * The default version of this member always returns EOF.
  705.  
  706.     void setg(char *base, char *get, char *end)
  707.     {
  708.         _gbase = base;
  709.         _gptr = get;
  710.         _egptr = end;
  711.     }
  712. // Sets the base of the get area, the get pointer, and the end of
  713. // the get area.
  714.  
  715.     void setp(char *base, char *end)
  716.     {
  717.         _pbase = _pptr = base;
  718.         _epptr = end;
  719.     }
  720. // Sets the base of the put area, the put pointer, and the end of
  721. // the put area.
  722.  
  723.     void setb(char *base, char *end, int own = 0);
  724. // Sets the base and end of the buffer. If own is true, then this
  725. // streambuf will delete base in its destructor or if setb() is called
  726. // again.
  727.  
  728.     void gbump(int n) { _gptr += n; }
  729.     void pbump(int n) { _pptr += n; }
  730.  
  731.     int unbuffered() const { return _unbuffered; }
  732.     void unbuffered(int u) { _unbuffered = u; }
  733.  
  734.  
  735. private:
  736.     char *_base;
  737.     char *_ebuf;
  738.     char *_gbase;
  739.     char *_gptr;
  740.     char *_egptr;
  741.     char *_pbase;
  742.     char *_pptr;
  743.     char *_epptr;
  744.     char _unbuffered;
  745.     char _owned;
  746.  
  747.     char unbuf[2];
  748. // This is big enough to deal with the translated case when unbuffered
  749.  
  750.     int     sbumpc_special();
  751. // Called by sbumpc() when get_pointer == get_area_end.
  752. // Simply calls overflow(), increments the get pointer, and
  753. // returns the value returned by overflow(). It's here so that
  754. // the special-case code does not have to be inlined.
  755. };
  756.  
  757. class ostream : virtual public ios {
  758.  
  759. // This is the class used for formatted character output.
  760. // The various "operator << (...)" members are called "inserters", because
  761. // they insert information into the character stream.
  762. //
  763. // Unless otherwise noted, all of the functions here that do output set
  764. // the error state on failure.
  765. //
  766. // Where the inserters perform conversions of numbers to a readable string, 
  767. // the conversion is affected by the format state flags in ios::flags() .
  768.  
  769. public:
  770.     ostream(streambuf * buffer);
  771. // Construct an ostream associated with the argument streambuf.
  772.  
  773.     virtual ~ostream();
  774.  
  775.     ostream &flush();
  776. // Flush any characters queued for output.
  777.  
  778.     int opfx();
  779. // Perform output-prefix operations.
  780. // If the error state is non-zero, return zero immediately.
  781. // If there is a tied ostream (see ios::tie()), flush it.
  782. // Return non-zero. If you define your own inserter that directly
  783. // manipulates the streambuf instead of calling other inserters,
  784. // it should call this when it starts.
  785.  
  786.     void osfx();
  787. // Perform output-suffix operations.
  788. // If ios::unitbuf is set in the format state, flush output.
  789. // If ios::stdio is set, flush stdio and stderr.
  790. // If you define your own inserter that directly manipulates the
  791. // streambuf, it should call this just before returning.
  792. // All of the inserters in ostream call this.
  793. // The binary put() and write() functions do not call this.
  794.  
  795.     ostream &put(char c);
  796. // Inserts a character
  797.  
  798.     ostream &seekp(streampos position);
  799. // Absolute seeks the output stream to "position".
  800.  
  801.     ostream &seekp(streamoff offset, seek_dir direction);
  802. // Relative seeks the output stream. See streambuf::seekoff()
  803.  
  804.     streampos tellp();
  805. // Returns the current position in the output stream.
  806.  
  807.     ostream &write(const void *data, size_t size);
  808.     size_t pcount() { return write_count; }
  809. // Inserts the block of "size" bytes starting at "data". Does not
  810. // perform any formatting. The number of characters successfully
  811. // output can be determined by a following call to ostream::pcount().
  812.  
  813.     ostream &operator<<(const char *string);
  814.     ostream &operator<<(const signed char *string)
  815.         { return operator<<((const char *) string); }
  816.     ostream &operator<<(const unsigned char *string)
  817.         { return operator<<((const char *) string); }
  818. // Insert a null-terminated string.
  819.  
  820.     ostream &operator<<(char c);
  821.     ostream &operator<<(signed char c)
  822.         { return operator<<((char) c); }
  823.     ostream &operator<<(unsigned char c)
  824.         { return operator<<((char) c); }
  825. // Insert a single character.
  826.  
  827.     ostream &operator<<(short v)
  828.         { return _2Comp::insert(*this,&v,sizeof(short),1); }
  829.     ostream &operator<<(int v)
  830.         { return _2Comp::insert(*this,&v,sizeof(int),1); }
  831.     ostream &operator<<(long v)
  832.         { return _2Comp::insert(*this,&v,sizeof(long),1); }
  833.     ostream &operator<<(unsigned short v)
  834.         { return _2Comp::insert(*this,&v,sizeof(unsigned short),0); }
  835.     ostream &operator<<(unsigned v)
  836.         { return _2Comp::insert(*this,&v,sizeof(unsigned),0); }
  837.     ostream &operator<<(unsigned long v)
  838.         { return _2Comp::insert(*this,&v,sizeof(unsigned long),0); }
  839.  
  840. #if macintosh
  841.     ostream &operator<<(float v) { return operator<<((long double) v); }
  842.     ostream &operator<<(double v) { return operator<<((long double) v); }
  843.     ostream &operator<<(long double);
  844. #else
  845.     ostream &operator<<(float v) { return operator<<((double) v); }
  846.     ostream &operator<<(double);
  847. #endif
  848.  
  849.     ostream &operator<<(void *);
  850. // Convert a pointer to a hex integer, and insert.
  851.  
  852.     ostream &operator<<(streambuf *source);
  853. // Insert all of the characters that can be fetched from the streambuf.
  854.  
  855.     ostream &operator<<(ostream &(*manipulator)(ostream &))
  856.         { return (*manipulator)(*this); }
  857. // Parameterless manipulators are implemented by providing an
  858. // inserter for the type pointer to function taking an ostream reference
  859. // argument and returning an ostream reference.  The specified function
  860. // is simple executed for the current ostream.
  861.  
  862.     ostream &operator<<(ios &(*manipulator)(ios &))
  863.     {
  864.         (*manipulator)(*this);
  865.         return *this;
  866.     }
  867. // Similar facility to insert a pointer to function taking an ios
  868. // reference argument.
  869.  
  870. // This isn't in the draft standard, but it is so useful in the
  871. // implementation of inserters that it is made public here.
  872.     int pad(int where, int wide);
  873.  
  874. protected:
  875.     ostream();  // ios will be initialized by derived class
  876. private:
  877.     size_t write_count;
  878. // Records number of bytes actually put out by a write()
  879.  
  880.     void eof_fail() { clear(ios::badbit | ios::failbit | ios:: eofbit); }
  881. // Internal. Set error flags after an output EOF error.
  882.  
  883. #if macintosh
  884.     int fixed_point(long double, char *);
  885. #else
  886.     int fixed_point(double, char *);
  887. #endif
  888. // Internal. Convert double to fixed-point string.
  889.  
  890. #if macintosh
  891.     int sci_notation(long double, char *, int &);
  892. #else
  893.     int sci_notation(double, char *, int &);
  894. #endif
  895. // Internal. Convert double to scientific-notation string.
  896.  
  897.     int adjust(int bit_to_test, int field_width);
  898. // Manages padding
  899.     void flush_stdio();
  900. // Called by osfx() if ios::stdio is set.  This function is in a
  901. // separate module to prevent the stdio stuff from being pulled
  902. // in when it is not required.
  903. };
  904.  
  905. // The following functions provide parameterless inserters of the
  906. // type noted above. They have to be real functions because we
  907. // need to take their address.
  908.  
  909. ostream &ends(ostream&);
  910. // Ends a string by inserting a null character. Use this on strstreams
  911. // before you call ostrstream::freeze() .
  912. //
  913. // stream << "whatever" << ends;
  914.  
  915. ostream &flush(ostream&);
  916. // Flushes output on an ostream.
  917. //
  918. // stream << "whatever" << flush
  919.  
  920. ostream &endl(ostream &);
  921. // Inserts the appropriate new-line character(s) for the system, and flushes
  922. // output. On Unix the new-line character is inserted. On DOS, the
  923. // carriage-return and new-line characters are inserted. Thus, using endl
  924. // instead of '\n' or "\r\n" is more portable.
  925. //
  926. // stream << "whatever" << endl;
  927.  
  928. ostream &stickywidth(ostream &);
  929. // Sets the state so that width is not reset after each insertion
  930. // unlatch this by a call to width() or use setw manipulator.
  931.  
  932. ostream &spacing(ostream &);
  933. ostream &nospacing(ostream &);
  934. // Sets the state so that a space is output after any item which
  935. // makes a call to osfx() except the endl manipulator.
  936.  
  937. ostream &fixed(ostream &);
  938. ostream &scientific(ostream &);
  939. ostream &showpoint(ostream &);
  940. ostream &floating(ostream &);
  941. ostream &uppercase(ostream &);
  942. // Manipulate the floating point format, floating sets the default
  943. // state.
  944.  
  945. ostream &leftjust(ostream &);
  946. ostream &rightjust(ostream &);
  947. ostream &internal(ostream &);
  948. ostream &showbase(ostream &);
  949. // Set a justify mode
  950.  
  951. // types used by istream dfa extractor function
  952. typedef void (*translate_function_t)(void *, const char *, void *);
  953. typedef int (*helper_function_t)(int, char, void *);
  954.  
  955. class istream : virtual public ios {
  956.  
  957. // This is the class used for character input.
  958. // The various "operator>>(...)" members are called "extractors", because
  959. // they extract information from the character stream.
  960. //  * Where the extractors perform conversions of a string to a number,
  961. // the conversion is affected by the format state flags in ios::flags() .
  962. // Each of the extractors calls ipfx() first, and returns immediately if
  963. // ipfx returns zero. Extractors indicate errors by setting bits of the
  964. // error state. ios::failbit means the input characters weren't a
  965. // representation of the required type. ios::badbit means that the I/O
  966. // system failed to get the required characters.
  967. //  * The unformatted extractors (get(), getline()) will set ios::failbit
  968. // only if they are not able to extract at least one character.
  969. //  * Caveat: The ATT version ignores overflow. This version attempts to set
  970. // the error flags on overflow.
  971.  
  972. public:
  973.     static int is_white_space(char c);
  974. // Internal. Return 1 if the argument character is white-space,
  975. // otherwise return 0.
  976.  
  977.     istream(streambuf *buffer);
  978. // Construct a new istream associated with the argument streambuf.
  979.  
  980.     virtual ~istream();
  981.  
  982.     size_t gcount() { return read_count; }
  983. // Return the number of characters extracted by the last unformatted
  984. // input function. Other input functions may call the unformatted
  985. // input functions, so this counter is only accurate immediately
  986. // after a call to an unformatted input function.
  987.  
  988.     istream &get(char *data, int length, char delimiter = '\n');
  989.     istream &get(signed char *data, int length, char delimiter = '\n');
  990.     istream &get(unsigned char *data, int length, char delimiter = '\n');
  991. // Both of these extract up to "length" characters, storing them in
  992. // "data". If a character equal to "delimiter" is seen, it is pushed
  993. // back on the input stream and extraction stops.
  994.  
  995.     istream &get(char &destination);
  996.     istream &get(signed char &destination);
  997.     istream &get(unsigned char &destination);
  998. // Extract a single character.
  999.  
  1000.     istream &get(streambuf &destination, char delimiter = '\n');
  1001. // Call ipfx(0), and if the result is non-zero, get characters until
  1002. // the delimiter is seen or EOF, and stuff the characters into the
  1003. // streambuf. Doesn't extract the delimiter character. Only sets
  1004. // ios::badbit if it can't extract at least one character.
  1005.  
  1006.     int get();
  1007. // Get one character, and return it in an int. Return EOF if there's
  1008. // an error.
  1009.  
  1010.     istream &getline(char *data, int length, char delimiter = '\n');
  1011.     istream &getline(signed char *data, int length, char delimiter = '\n');
  1012.     istream &getline(unsigned char *data, int length, char delimiter = '\n');
  1013. // These two are like get(char *data, int length, char delimiter),
  1014. // except that they extract the terminating delimiter instead of
  1015. // pushing it back.
  1016.  
  1017.     istream &ignore(int length = 1, int delimiter = EOF);
  1018. // Extract and throw away up to "length" characters. Extraction
  1019. // stops if "delimiter" is extracted, or at EOF. If "delimiter"
  1020. // is EOF, it won't match any character, and thus will not stop
  1021. // extraction.
  1022.  
  1023.     int ipfx(int need = 0);
  1024. // Perform input-prefix operations.
  1025. // The argument "need" is the number of characters that will be
  1026. // required, or 0 if you don't know how many you'll need. The
  1027. // formatted input functions call this with 0, and the unformatted
  1028. // input functions call it with 1.
  1029. //      * If the error state is non-zero, this returns zero immediately.
  1030. // Otherwise, it returns non-zero when finished.
  1031. // If there is a tied stream (see ios::tie()) and need is 0 or greater
  1032. // than the number of immediately available characters, the tied
  1033. // iostream is flushed.
  1034. //      * If ios::skipws is set in the format state, and "need" is zero,
  1035. // leading white-space characters are thrown away and the stream
  1036. // is advanced to the first available character.
  1037. // This returns zero if there is an error while skipping a
  1038. // white-space character, otherwise it returns non-zero.
  1039.  
  1040.     int peek();
  1041. // Calls ipfx(1). If that returns zero, or if the input character
  1042. // stream is at EOF, return EOF. Otherwise return the next character
  1043. // without extracting it from the input stream.
  1044.  
  1045.     istream &putback(char c);
  1046. // Attempt to push the last character read back onto the input stream.
  1047. // "c" must be the last character read. This does not call ipfx(), but
  1048. // it will return without doing anything if the error state is
  1049. // non-zero.
  1050.  
  1051.     istream &read(void *data, int size);
  1052. // Extracts a block of "size" bytes and stores them at "data".
  1053. // If EOF is reached before "size" bytes are extracted,
  1054. // ios::failbit is set. The number of characters actually
  1055. // extracted is available as the return value of istream::gcount().
  1056.  
  1057.     istream &seekg(streampos position);
  1058. // Absolute-seek the input character stream to "position".
  1059.  
  1060.     istream &seekg(streamoff offset, seek_dir direction);
  1061. // Relative-seek the input character stream by "offset" relative to
  1062. // the current position.
  1063.  
  1064.     int sync();
  1065. // Establishes consistency between the internal data structure
  1066. // and the external character source. This usually means that
  1067. // characters that were buffered for input are thrown away, and
  1068. // the file pointer is decremented so that it appears that the
  1069. // buffered characters were never read.
  1070.  
  1071.     streampos tellg();
  1072. // Return the current file position. The value returned is a magic
  1073. // cookie that is usable only as an argument to seekg(streampos).
  1074. // Don't perform arithmetic on the return value.
  1075.  
  1076.     istream &operator>>(char *);
  1077.     istream &operator>>(signed char *s)
  1078.         { return operator>>((char *) s); }
  1079.     istream &operator>>(unsigned char *s)
  1080.         { return operator>>((char *) s); }
  1081. // For the string extractors, characters are extracted until a
  1082. // white-space is seen. The white-space is pushed back on the
  1083. // incoming character stream. If width() is non-zero, it is taken
  1084. // as the size of the argument character array, and no more than
  1085. // width()-1 characters are extracted. A terminating null character
  1086. // is ALWAYS stored, even when nothing else is done because of
  1087. // the error state.
  1088.  
  1089.     istream &operator>>(char &);
  1090. // Extracts a single character, similar to get()
  1091. #if macintosh
  1092.     istream &operator>>(signed char &c)
  1093.         { return operator>>((char &) c); }
  1094.     istream &operator>>(unsigned char &c)
  1095.         { return operator>>((char &) c); }
  1096. #else
  1097.     istream &operator>>(signed char &c)
  1098.         { return _2Comp::extract(*this,&c,1); }
  1099.     istream &operator>>(unsigned char &c)
  1100.         { return _2Comp::extract(*this,&c,1); }
  1101. #endif
  1102.  
  1103. // Extract byte wide integers -128 - +127 and 0 - 255 respectively
  1104.  
  1105.     istream &operator>>(short &v)
  1106.         { return _2Comp::extract(*this,&v,sizeof(short)); }
  1107.     istream &operator>>(int &v)
  1108.         { return _2Comp::extract(*this,&v,sizeof(int)); }
  1109.     istream &operator>>(long &v)
  1110.         { return _2Comp::extract(*this,&v,sizeof(long)); }
  1111.     istream &operator>>(unsigned short &v)
  1112.         { return _2Comp::extract(*this,&v,sizeof(unsigned short)); }
  1113.     istream &operator>>(unsigned int &v)
  1114.         { return _2Comp::extract(*this,&v,sizeof(unsigned int)); }
  1115.     istream &operator>>(unsigned long &v)
  1116.         { return _2Comp::extract(*this,&v,sizeof(unsigned long)); }
  1117.  
  1118.     istream &operator>>(float &);
  1119.     istream &operator>>(double &);
  1120. #if macintosh
  1121.     istream &operator>>(long double &);
  1122. #endif
  1123.  
  1124.     istream &operator>>(streambuf*);
  1125. // Calls ipfx(0), and if the result is non-zero, extracts characters
  1126. // and stuffs them into the argument streambuf until EOF is reached.
  1127. // Only sets ios::badbit if it can't get at least one character.
  1128.  
  1129.     istream &operator>>(istream &(*manip)(istream&))
  1130.     {
  1131.         (*manip)(*this);
  1132.         return *this;
  1133.     }
  1134. // Provides an pseudo extractor which allows for parameterless
  1135. // manipulators. The target type is pointer to function returning
  1136. // istream reference and taking istream reference argument.
  1137.  
  1138.     istream &operator>>(ios &(*manip)(ios&))
  1139.     {
  1140.         (*manip)(*this);
  1141.         return *this;
  1142.     }
  1143. // Similar facility to manipulate an ios object directly
  1144.  
  1145. protected:
  1146.     istream();  // derived class will initialize ios directly
  1147.  
  1148. private:
  1149. friend istream &_2Comp::extract(istream&, void *, int);
  1150.  
  1151. // The extract function provides for general type translation using
  1152. // a character set string and a DFA transition table
  1153.     istream &extract(void *type, const char *okchars, int *tt,
  1154.             translate_function_t tf, helper_function_t hf, void *tds);
  1155.     size_t read_count;
  1156. };
  1157.  
  1158. istream &ws(istream&);
  1159. // Manipulator to skip white-space, positioning the input
  1160. // character stream to the first available non-white-space
  1161. // character.
  1162.  
  1163. class iostream : public istream, public ostream {
  1164.  
  1165. // A stream that supports both insertion and extraction.
  1166.  
  1167. public:
  1168.     iostream(streambuf *buf);
  1169. // Construct an iostream associated with the argument streambuf.
  1170.  
  1171.     virtual ~iostream();
  1172. protected:
  1173.     iostream(); // derived class will initialize ios directly
  1174.  
  1175. };
  1176.  
  1177. // Iostream classes that support assignment.
  1178. //
  1179. // They're used for cin, cout, cerr, and clog, but you should not use them for
  1180. // anything else. Instead, use a regular stream and assign a pointer to it.
  1181.  
  1182. class istream_withassign : public istream {
  1183. public:
  1184.     istream_withassign();
  1185.     istream_withassign(streambuf *);
  1186.     ~istream_withassign();
  1187.     istream_withassign &operator=(istream &);
  1188.     istream_withassign &operator=(istream_withassign &s) {return operator=((istream &)s);}
  1189.     istream_withassign &operator=(streambuf *);
  1190. private:
  1191.     short assigned_to;
  1192. };
  1193.  
  1194. class ostream_withassign : public ostream {
  1195. public:
  1196.     ostream_withassign();
  1197.     ostream_withassign(streambuf *);
  1198.     ~ostream_withassign();
  1199.     ostream_withassign &operator=(ostream &);
  1200.     ostream_withassign &operator=(ostream_withassign &s) {return operator=((ostream &)s);}
  1201.     ostream_withassign &operator=(streambuf *);
  1202. private:
  1203.     short assigned_to;
  1204. };
  1205.  
  1206. class iostream_withassign : public iostream {
  1207. public:
  1208.     iostream_withassign();
  1209.     iostream_withassign(streambuf *);
  1210.     ~iostream_withassign();
  1211.     iostream_withassign &operator=(ios &);
  1212.     iostream_withassign &operator=(iostream_withassign &s) {return operator=((ios &)s);}
  1213.     iostream_withassign &operator=(streambuf *);
  1214. private:
  1215.     short assigned_to;
  1216. };
  1217.  
  1218. extern istream_withassign cin;
  1219. extern ostream_withassign cout;
  1220. extern ostream_withassign cerr;
  1221. #ifdef __XENIX__
  1222. extern ostream_withassign   clog;
  1223. #endif
  1224.  
  1225. class IosTie {
  1226. public:
  1227.     IosTie(ios *a, ostream *b) {a->tie(b);
  1228. #if THINK_CPLUS
  1229.                                 ios::sync_with_stdio();
  1230. #endif
  1231.                                 }
  1232. };
  1233.  
  1234. class IosUnitbuf {
  1235. public:
  1236.     IosUnitbuf(ios *a) { a->setf(ios::unitbuf); }
  1237. };
  1238.  
  1239. #endif  // __IOSTREAM_H
  1240.